package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo08 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("输入用户名");
        String username=sc.nextLine();
        System.out.println("输入密码");
        String password=sc.nextLine();

        try(Connection conn=DBUtils.getConn()){
//            Statement s=conn.createStatement();
//            //准备登录SQL语句
//            String sql="select count(*) from user where "+"username='"+username+"' and password='"+password+"'";
//            //执行查询
//            ResultSet rs=s.executeQuery(sql);
            String sql="select count(*) form user where username=? and password=?";
            //创建有预翻译功能的 执行SQL语句的对象
            PreparedStatement ps=conn.prepareStatement(sql);
            //替代SQL语句中的?
            ps.setString(1,username);
            ps.setString(2,password);
            //执行SQL语句
            ResultSet rs=ps.executeQuery();
            //游标下移 指向查询到的数量
            rs.next();
            int count = rs.getInt(1);
            //判断大于0 说明存在即登录成功 反之失败
            if(count>0){
                System.out.println("登录成功");
            }else{
                System.out.println("登陆失败");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
